package com.jbh.checkpermissionlibaray;


import android.Manifest;
import android.content.ContentValues;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.jbh.permissionlibrary.base.BasePermissionActivity;
import com.jbh.permissionlibrary.i.AgreePermissionListener;
import com.jbh.permissionlibrary.i.CheckPermissionListener;
import com.jbh.permissionlibrary.PermissionManager;
import com.jbh.permissionlibrary.i.PermissionRequestCode;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    public static final String TAG="checkpermissionlibarayMainActivity";
    TextView btn_permission ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG,"onCreate");
        setContentView(R.layout.activity_main);
        btn_permission =  findViewById(R.id.btn_permission);
        btn_permission.setText("申请权限");
        btn_permission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<String> list = new ArrayList<>();
                list.add(Manifest.permission.CAMERA);
                list.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                PermissionManager.newInstance().checkPermission(MainActivity.this,
                        list, PermissionRequestCode.WRITE_EXTERNAL_STORAGE_AND_CAMERA_REQUEST_CODE,new CheckPermissionListener() {
                    /**
                     * 不需要授权
                     */
                    @Override
                    public void onNoNeedCheckPermission() {

                    }
                });

//                ContentValues values = new ContentValues();
//                getContentResolver().insert(Uri.parse("content://com.jbh.threaddemo.DicContentProvider/"),values);
            }
        });
        if (savedInstanceState!=null){
            ((TextView)findViewById(R.id.btn_permission)).setText(savedInstanceState.getString("dfadaf")); ;
        }


        Drawable p = getResources().getDrawable(R.drawable.ic_launcher_background);
        Drawable n = getResources().getDrawable(R.mipmap.ceshi_ad);
        StateListDrawable drawable = new StateListDrawable();
        drawable.addState(new int[]{
                android.R.attr.state_pressed
        },p);
        drawable.addState(new int[]{},n);
        ((TextView)findViewById(R.id.btn_permission)).setBackground(drawable);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        /*切换横竖屏  保存数据*/
        outState.putString("dfadaf","测试横竖屏");
    }



    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        /**
         * 横屏
         */
        if (this.getResources().getConfiguration().orientation  == Configuration.ORIENTATION_LANDSCAPE){
            Log.v(TAG,"ORIENTATION_LANDSCAPE");
        }

        /**
         * 竖屏
         */
        if (this.getResources().getConfiguration().orientation  == Configuration.ORIENTATION_PORTRAIT){
            Log.v(TAG,"ORIENTATION_PORTRAIT");
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.v(TAG,"onStart");
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Log.v(TAG,"onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.v(TAG,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.v(TAG,"onPause");
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.v(TAG,"onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.v(TAG,"onDestroy");
    }



    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionManager.newInstance().setPermissionsResult(this, requestCode, permissions, grantResults, new AgreePermissionListener() {
            @Override
            public void onAgreePermission(int requestCode) {
                AgreePermission(requestCode);
            }
        });
    }



    /**
     * 点击同意权限
     *
     * @param requestCode
     */
    protected void AgreePermission(int requestCode) {
        switch (requestCode){
            case PermissionRequestCode.REQUEST_CODE:
                break;
            case PermissionRequestCode.CALL_PHONE_REQUEST_CODE:
                break;
            case PermissionRequestCode.CAMERA_REQUEST_CODE:
                break;
            case PermissionRequestCode.WRITE_EXTERNAL_STORAGE_REQUEST_CODE:
                break;
            case PermissionRequestCode.RECORD_AUDIO_REQUEST_CODE:
                break;
        }
    }
}